﻿using System;
using System.Messaging;
using System.Threading;

namespace TripRemoteObject
{
    public class TripChanges : MarshalByRefObject
    {
        readonly string QueuePaymentClientSend = Properties.Settings.Default.QueuePaymentClientSend;
        
        public AmountState GetAmountState(int userId, decimal distance, bool status)
        {
            AutoResetEvent resetEvent = new AutoResetEvent(false);;
            MessageQueue msMq;
            
            msMq = new MessageQueue(QueuePaymentClientSend);
            long idOperacion = 0;
            
            try
            {
                idOperacion = MessageQueueMgr.Instance.AddOperation(userId, resetEvent);
                
                string estado = status ? "E" : "F";
                var operationMessage = idOperacion + "|" + userId + "|" + distance.ToString("0,0.000") + "|" + estado;
                msMq.Send(operationMessage);
            }
            catch (MessageQueueException ee)
            {
                Console.Write(ee.ToString());
                msMq.Close();
            }
            catch (Exception eee)
            {
                Console.Write(eee.ToString());
                msMq.Close();
            }

            return MessageQueueMgr.Instance.GetAmountState(idOperacion, resetEvent);
        }

    }
}
